#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: api03_metadataInstances.py 
@version:
@time: 2022/03/01 
"""
import json

from TapClientCore.T02_tm_api.v2_api.v2_api_base import *


class Api03MetaDataInstancesUrl(V2ApiUrlBase):
    url_metadataTransformerItems = api_joint(V2ApiUrlBase().server, METADATA_TRANSFORMERITEMS)
    url_metadataInstances = api_joint(V2ApiUrlBase().server, METADATA_INSTANCES)
    url_metadataInstances_originalData = api_joint(url_metadataInstances, META_ORIGINAL_DATA)
    url_metadataInstances_update = api_joint(url_metadataInstances, META_UPDATE)
    url_metadataInstances_find_inspect = api_joint(url_metadataInstances, META_FIND_INSPECT)
    url_metadataInstances_find_table_by_id = api_joint(url_metadataInstances, META_FIND_TABLE_BY_ID)
    url_metadataInstances_search = api_joint(url_metadataInstances, META_SEARCH)
    url_metadataInstances_table_connection_id = api_joint(url_metadataInstances, META_TABLE_CONNECTION_ID)
    url_metadataInstances_node_schema = api_joint(url_metadataInstances, 'node', 'schema')


class Api03MetaDataInstance(V2ApiBase):

    # 模型推演
    def get_metadataTransformerItems(self, filter):
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataTransformerItems
            + self.access_token
            + self.filter(filter)
        )
        return response

    # 获取模型
    def get_metadataInstances_originalData(self, qualified_name):
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataInstances_originalData
            + f'?qualified_name={qualified_name}'
            + self.access_token2
        )
        return response

    # 更新模型
    def post_metadataInstances_update(self, where, payload):
        response = self.tapclient(
            self.post,
            Api03MetaDataInstancesUrl.url_metadataInstances_update
            + self.where(where)
            + self.access_token2,
            json=payload
        )
        return response

    # 弃用 获取数据校验的模型
    def get_metadataInstances_findInspect(self, _filter):
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataInstances_find_inspect
            + self.access_token
            + self.filter(_filter)
        )
        return response

    # 获取数据校验的模型数据
    def post_metadataInstances_findInspect(self, payload):
        response = self.tapclient(
            self.post,
            Api03MetaDataInstancesUrl.url_metadataInstances_find_inspect
            + self.access_token,
            json=payload
        )
        return response

    # 通过filter筛选模型
    def get_metadataInstances_by_filter(self, filter):
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataInstances
            + self.access_token
            + self.filter(filter)
        )
        return response

    # 通过模型id删除模型
    def delete_metadataInstances_by_id(self, meta_id):
        response = self.tapclient(
            self.delete,
            api_joint(Api03MetaDataInstancesUrl.url_metadataInstances, meta_id)
            + self.access_token
        )
        return response

    # 通过表id获取表模型
    def get_metadataInstances_find_table_by_id(self, meta_id):
        response = self.tapclient(
            self.get,
            api_joint(
                Api03MetaDataInstancesUrl.url_metadataInstances_find_table_by_id,
                meta_id
            )
            + self.access_token
        )
        return response

    # 修改模型
    def patch_metadataInstances_by_id(self, meta_id, payload):
        response = self.tapclient(
            self.patch,
            api_joint(
                Api03MetaDataInstancesUrl.url_metadataInstances,
                meta_id
            )
            + self.access_token,
            json=payload
        )
        return response

    # 数据发现-数据搜索接口
    def get_metadataInstances_search(self, meta_type, keyword, last_id=None):
        lastId = 'lastId'
        if last_id:
            lastId += f'={last_id}'
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataInstances_search
            + self.access_token
            + f'&type={meta_type}&keyword={keyword}&pageSize=50&{lastId}'
        )
        return response

    # 通过数据源id获取表名
    def get_metadataInstances_table_by_connection_id(self, connection_id):
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataInstances_table_connection_id + connection_id
            + self.access_token2
        )
        return response

    # 数据开发，获取节点模型
    def get_metadataInstances_node_schema(self, nodeId):
        """
        :param nodeId:
        :return: data:[{fields, indices}]
        """
        response = self.tapclient(
            self.get,
            Api03MetaDataInstancesUrl.url_metadataInstances_node_schema
            + self.access_token
            + f'&nodeId={nodeId}&fields[]=fields'
        )
        return response


if __name__ == '__main__':
    T = Api03MetaDataInstance()
    print(T.get_metadataInstances_node_schema('ff5058e9-270d-4e95-a88d-a10884816441').get('data'))
